package xdrawer.shape;

import javafx.geometry.VPos;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.shape.Polygon;
import javafx.scene.text.TextAlignment;

/**
 * 形状类, 包括:处理框、判定框、数据框、开始结束、连接
 * 所有形状都定义为一个矩形区域，由x,y,width,height确定
 */
public class Symbol extends AbstractSymbol {
    private static final double SELECTED_BOX_GAP = 5.0;
    private static final double SELECTED_BOX_SIZE = 5.0;

    private int symbolType;

    private double x;
    private double y;
    private double width;
    private double height;
    private String text;

    private boolean isSelected = false;
    // 存储开始拖动时，符号的x,y与鼠标位置的距离
    private double dxToPointer, dyToPointer;

    public Symbol(int symbolType, double x, double y, double width, double height, String text) {
        this.symbolType = symbolType;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.text = text;
    }

    @Override
    public boolean containsPoint(double x, double y) {
        return x > this.x && x < this.x + this.width && y > this.y && y < this.y + this.height;
    }

    @Override
    public void draw(GraphicsContext gc) {
        gc.setFill(GlobalConfig.CANVAS_BACKGROUND_COLOR);
        gc.setStroke(GlobalConfig.STROKE_COLOR);
        gc.setLineWidth(GlobalConfig.LINE_WIDTH);
        gc.setTextAlign(TextAlignment.CENTER);
        gc.setTextBaseline(VPos.CENTER);


        switch (symbolType) {
            // 处理框
            case SymbolType.PROCESS:
                gc.fillRect(x, y, width, height);
                gc.strokeRect(x, y, width, height);
                gc.strokeText(text, x + width / 2, y + height / 2);
                break;
            // 开始结束框
            case SymbolType.START_END:
                gc.fillRoundRect(x, y, width, height, height / 2, height / 2);
                gc.strokeRoundRect(x, y, width, height, height / 2, height / 2);
                gc.strokeText(text, x + width / 2, y + height / 2);
                break;
            // 条件框
            case SymbolType.CONDITION:
                double[] xPoints = {x + width / 2, x + width, x + width / 2, x,};
                double[] yPoints = {y, y + height / 2, y + height, y + height / 2,};
                gc.fillPolygon(xPoints, yPoints, 4);
                gc.strokePolygon(xPoints, yPoints, 4);
                gc.strokeText(text, x + width / 2, y + height / 2);
                break;

        }
    }

    // 绘制当前对象的选中框
    public void drawSelectedBox(GraphicsContext gc) {
        gc.setLineDashes(4);
        gc.setStroke(GlobalConfig.SELECTED_SYMBOL_COLOR);
        gc.setLineWidth(1);
//        gc.strokeRect(x - 3, y - 3, width + 6, height + 6);
        gc.strokeRect(x, y, width, height);
        gc.setLineDashes(null);
        gc.setLineWidth(GlobalConfig.LINE_WIDTH);


        gc.setFill(GlobalConfig.SELECTED_SYMBOL_COLOR);
        gc.fillRect(x - 3 + width / 2, y - 6, 5, 5);
    }

    // setters && getters--------------------------------------------
    public int getSymbolType() {
        return symbolType;
    }

    public void setSymbolType(int symbolType) {
        this.symbolType = symbolType;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }

    public double getDxToPointer() {
        return dxToPointer;
    }

    public void setDxToPointer(double dxToPointer) {
        this.dxToPointer = dxToPointer;
    }

    public double getDyToPointer() {
        return dyToPointer;
    }

    public void setDyToPointer(double dyToPointer) {
        this.dyToPointer = dyToPointer;
    }
}
